<template>
  <div class="wrapper">
    <div class="login-box">
      <div class="logo">
        <img src="../../assets/logo.png" height="120px" alt="" />
      </div>
      <!-- <h3 class="title">登录界面</h3> -->
      <div>{{ info }}</div>
      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="60px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="account">
          <el-input
            type="text"
            v-model="loginForm.account"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="apwd">
          <el-input
            type="password"
            v-model="loginForm.apwd"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validateUser = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      info: "",
      loginForm: {
        account: "",
        apwd: "",
      },
      rules: {
        account: [{ validator: validateUser, trigger: "blur" }],
        apwd: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const url = "http://localhost:4444/adminlogin";

          const params = `account=${this.loginForm.account}&apwd=${this.loginForm.apwd}`;
          console.log(params);

          this.$http.post(url, params).then((res) => {
            console.log(res); //如果错误，msg中会提示哪个参数错误
            if (res.data.code == 200) {
              console.log("登录成功", res.data.result.aname);
              this.$store.commit("updateAname", res.data.result.aname);
              this.$router.push("/home");
            } else {
              alert("登录失败" + res.data.message);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang='less' scoped>
.wrapper {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;
  background: #104468;
}
.login-box {
  width: 400px;
  height: 260px;
  padding: 20px;
  margin: 0 auto;
  margin-top: 180px;
  border-radius: 10px;

  border: 1px solid #eee;
  background: #fff;
  position: relative;
}
.logo {
  position: absolute;
  width: 120px;
  height: 120px;
  background: #f7f7f7;
  border-radius: 50%;
  overflow: hidden;
  top: -80px;
  left: 50%;
  margin-left: -60px;
  padding: 8px;
  img {
    border-radius: 50%;
    background: #fff;
  }
}
.demo-ruleForm {
  margin-top: 60px;
}
.title {
  margin-bottom: 30px;
  text-align: center;
  color: #666;
}
</style>